﻿package  
{
import org.papervision3d.core.math.Matrix3D;
import org.papervision3d.core.math.Number3D;
import org.papervision3d.core.math.Plane3D;
import org.papervision3d.materials.MovieMaterial
import flash.display.Shape
import flash.display.Sprite
import flash.display.Shape;
import flash.geom.Matrix; 
import flash.display.GradientType;
import flash.display.Graphics
/**
* ...
* @author Exey Panteleev
*/
public class ColorSpotCaster 
{
	public var p3d:Plane3D; // just to hold plane orientation
	public var spot:Shape = new Shape();
	
	public var spotColor:Number
	public var spotWidth:int
	public var spotHeight:int
	public var spotDepth:int
	
	public var sx:int
	public var sy:int
	public var sz:int
	
	public var movieFromMat:Sprite;
	
	public static const MIN_DISTANCE:uint = 400;
	
	public function ColorSpotCaster(plane3d:Plane3D, movieFromMaterial:Sprite, color:Number, width:int, height:int) 
	{
		this.p3d = plane3d
		this.movieFromMat = movieFromMaterial
		this.spotColor = color
		this.spotWidth = width
		this.spotHeight = height 
		this.spotDepth = width
		drawSpot();
	}
	
	
	public function drawSpot():void
	{
		 spot.name = "spot";
		 var gType:String = GradientType.RADIAL;  

		 var matrix:Matrix = new Matrix();   
		 matrix.createGradientBox(spotWidth,spotHeight,0,-spotWidth*0.5, -spotHeight*0.5);  

		 var gColors:Array = [spotColor, 0x000000];  
		 var gAlphas:Array = [1,0];  
		 var gRatio:Array = [0,255];  

		 var g:Graphics = spot.graphics
		 g.beginGradientFill(gType,gColors,gAlphas,gRatio,matrix);  
		 g.drawRect(-spotWidth*0.5, -spotHeight*0.5, spotWidth, spotHeight); 
		 movieFromMat.addChild(spot)
	}
	
	public function updateSpotPositions(relativeColorThingPosition:Number3D, planeSize:uint, distance:uint):void 
	{
		if (p3d.normal.y == 1) {
			spot.x = relativeColorThingPosition.x
			spot.y = planeSize-relativeColorThingPosition.z
		}else if (p3d.normal.z == 1) {
			spot.x = relativeColorThingPosition.x
			spot.y = planeSize-relativeColorThingPosition.y
			//trace(int(spot.x),  int(spot.y), "|", int(colorThingPosition.x), int(colorThingPosition.y))
		}else if (p3d.normal.x == 1) {
			spot.x = relativeColorThingPosition.z
			spot.y = planeSize-relativeColorThingPosition.y
		}
		
		spot.alpha = (MIN_DISTANCE - distance) / MIN_DISTANCE
		
		if(distance > MIN_DISTANCE*0.5)
			spot.scaleX = spot.scaleY =2+distance/100
		else
			spot.scaleX = spot.scaleY =2+(MIN_DISTANCE-distance)/100
	}
}
}